import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ChoiceCommon {
  static Container singleChoice(String title, List<Widget> widgets) {
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 20.w),
        height: 30.h,
        decoration: BoxDecoration(
            color: Colors.white.withOpacity(0),
            border: const Border(bottom: BorderSide(color: Colors.grey))),
        child: Row(
          children: [
            SizedBox(
              width: 70.w,
              child: Text(
                title,
                textAlign: TextAlign.start,
                style: TextStyle(
                    height: 1.0,
                    fontSize: 15.h,
                    color: Colors.green[500],
                    fontWeight: FontWeight.w800),
              ),
            ),
            Expanded(
                flex: 9,
                child: ListView(
                  padding: const EdgeInsets.all(0),
                  scrollDirection: Axis.horizontal,
                  children: widgets,
                )),
          ],
        ));
  }

  static Container multipleChoice(String title, List<Widget> widgets) {
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 20.w),
        decoration: const BoxDecoration(
            border: Border(bottom: BorderSide(color: Colors.grey))),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Container(
              width: 70.w,
              child: Text(
                title,
                textAlign: TextAlign.start,
                style: TextStyle(fontSize: 16.sp, color: Colors.green[500]),
              ),
            ),
            Expanded(
                child: Container(
                  child: Wrap(
                    // 设置行与行之间的间距
                    children: widgets,
                  ),
                ),
                flex: 9),
          ],
        ));
  }

  /// exclude 排除指定选项  当 title选中的时候  exclude 不应该选中

  static Widget buildMultipleRadio(
      dynamic value, String title, List<String> chocis, Function callBack,
      {List<String>? exclude, bool enable = true}) {
    return InkWell(
      onTap: enable
          ? () {
              if (exclude != null && !chocis.contains(value)) {
                for (var element in exclude) {
                  chocis.remove(element);
                }
              }
              callBack(!chocis.contains(value), value, chocis);
            }
          : null,
      child: Container(
        color: Colors.white.withOpacity(0),
        height: 25.h,
        margin: const EdgeInsets.fromLTRB(0, 0, 20, 0),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            CustomCheckbox(chocis.contains(value), 15.h),
            const SizedBox(
              width: 5.0,
            ),
            Text(
              title,
              style: TextStyle(
                  fontSize: 15.h,
                  color: enable ? Colors.green[500] : Colors.grey,
                  height: 1.0),
            ),
          ],
        ),
      ),
    );
  }

  static Widget buildRadio(
      dynamic value, String title, dynamic groupValue, Function callBack) {
    bool checked = false;
    if (groupValue == null && value == null) {
      checked = true;
    } else if (value != null && groupValue != null) {
      checked = value.toString() == groupValue.toString();
    }

    return InkWell(
      onTap: () {
        callBack(value);
      },
      child: Container(
        color: Colors.white.withOpacity(0),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            CustomCheckbox(
              checked,
              15.h,
              circular: true,
            ),
            SizedBox(
              width: 15.h,
            ),
            Text(
              title,
              style: TextStyle(
                  fontSize: 15.h, color: Colors.green[500], height: 1.0),
            ),
            SizedBox(
              width: 15.h,
            ),
          ],
        ),
      ),
    );
  }
}

class CustomCheckbox extends StatefulWidget {
  final bool isChecked;
  final double size;
  final bool circular;
  const CustomCheckbox(this.isChecked, this.size,
      {super.key, this.circular = false});
  @override
  CustomCheckboxState createState() => CustomCheckboxState();
}

class CustomCheckboxState extends State<CustomCheckbox> {
  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      width: widget.size,
      height: widget.size,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey),
        borderRadius:
            BorderRadius.circular(widget.circular ? widget.size : 5.0),
        color: widget.isChecked ? Colors.green[500] : Colors.transparent,
      ),
      duration: const Duration(milliseconds: 200),
      child: widget.isChecked
          ? Icon(
              Icons.check,
              color: Colors.white,
              size: widget.size * 0.8,
              weight: 0.9,
            )
          : null,
    );
  }
}
